package we_chat_group

import (
	"fmt"
	"os"
	"server/src/global"
	"server/src/server"
	"server/src/utils"
	"strconv"
	"time"
)

// 开始游戏指令
func StartGame(answerId int) {
	for _, v := range ROBOTS {
		if v.GroupInfo == nil || v.Bot == nil || v.Group == nil {
			continue
		}
		sendMsg := fmt.Sprintf("开始答题[%d]", answerId)
		v.Group.SendText(sendMsg)
	}
}

// 开始游戏指令
func StopBettingReqPre() {
	for _, v := range ROBOTS {
		if v.GroupInfo == nil || v.Bot == nil || v.Group == nil {
			continue
		}
		sendMsg := fmt.Sprintf("----封卷时间还剩30秒----")
		v.Group.SendText(sendMsg)
	}
}

// 停止答题
func StopBettingReq(answerId int) {
	for _, v := range ROBOTS {
		if v.GroupInfo == nil || v.Bot == nil || v.Group == nil {
			continue
		}
		now := time.Now()
		hour, min, sec := now.Hour(), now.Minute(), now.Second()
		sendMsg := fmt.Sprintf("当前时间[%02d:%02d:%02d]\n", hour, min, sec)
		sendMsg += fmt.Sprintf("停止答题[%d]\n", answerId)
		sendMsg += "---------------\n"

		//获取群中用户联合guess表查找答题人数
		groupUserInfo := server.ServerGroupUser.GetGroupUserCurrentBettingInfo(v.GroupInfo.Id, answerId)
		//获取具体信息
		sendMsg += fmt.Sprintf("答题人数:[%d]\n", len(groupUserInfo))
		var sumIntegral float64
		var userBettingStr string
		for _, gv := range groupUserInfo {
			fmt.Println("__gv__", gv)
			userBettingStr += fmt.Sprintf("◆【%s】💰%.2f\n", gv.Name, gv.SumScore)
			userBettInfo, _ := server.ServerGroupUser.QueryBgCurrentBetting(gv.UserId, answerId)
			if userBettInfo == nil {
				continue
			}
			for _, uv := range userBettInfo.BgGuessNums {
				sumIntegral += uv.Integral
				switch uv.PlayingMethod {
				case global.BG_JIAO:
					userBettingStr += strconv.Itoa(uv.Num[0]) + strconv.Itoa(uv.Num[1])
					userBettingStr += "角/" + strconv.FormatFloat(uv.Integral, 'f', 0, 64) + " "
				case global.BG_ZHENG:
					userBettingStr += strconv.Itoa(uv.Num[0])
					userBettingStr += "正/" + strconv.FormatFloat(uv.Integral, 'f', 0, 64) + " "

				case global.BG_FAN:
					userBettingStr += strconv.Itoa(uv.Num[0])
					userBettingStr += "番/" + strconv.FormatFloat(uv.Integral, 'f', 0, 64) + " "
				case global.BG_NIAN:
					userBettingStr += strconv.Itoa(uv.Num[0]) + "念" + strconv.Itoa(uv.Num[1])
					userBettingStr += "/" + strconv.FormatFloat(uv.Integral, 'f', 0, 64) + " "
				}
			}
			userBettingStr += "\n\n"
		}
		sendMsg += fmt.Sprintf("答题总分:[%.2f]\n", sumIntegral)
		sendMsg += userBettingStr
		sendMsg += "---------------\n"
		sendMsg += fmt.Sprintf("停止答题[%d]\n", answerId)
		sendMsg += "因封号、封群、消息屏蔽等其它故障或原因造成的问题，为避免争执，一切以机器人所录入数据为准，请知悉。"
		v.Group.SendText(sendMsg)
	}
}

func GameEndResult(answerId, superNum, answerNum int) {
	for _, v := range ROBOTS {
		if v.GroupInfo == nil || v.Bot == nil || v.Group == nil {
			continue
		}
		now := time.Now()
		hour, min, sec := now.Hour(), now.Minute(), now.Second()
		sendMsg := fmt.Sprintf("当前时间[%02d:%02d:%02d]\n", hour, min, sec)
		sendMsg += fmt.Sprintf("答案公布[%d]\n", answerId)
		//获取群中用户联合guess表查找答题人数
		groupUserInfo := server.ServerGroupUser.GetGroupUserCurrentBettingInfo(v.GroupInfo.Id, answerId)
		//获取具体信息
		sendMsg += fmt.Sprintf("答题人数:[%d]\n", len(groupUserInfo))
		var sumIntegral float64
		var usersBettingStr string
		for _, gv := range groupUserInfo {
			var userBettingStr string
			userBettInfo, _ := server.ServerGroupUser.QueryBgCurrentBetting(gv.UserId, answerId)
			userPhase := 0.00
			for _, uv := range userBettInfo.BgGuessNums {
				numPhase := 0.00
				switch uv.PlayingMethod {
				case global.BG_JIAO:
					userBettingStr += strconv.Itoa(uv.Num[0]) + strconv.Itoa(uv.Num[1]) + "角"
					if answerNum == uv.Num[0] || answerNum == uv.Num[1] {
						bgOod := global.BG_ODDS_JIAO - 1
						numPhase += float64(bgOod) * uv.Integral
						numPhase -= float64(bgOod) * uv.Integral * v.GroupInfo.Pump
						userBettingStr += fmt.Sprintf("[%.2f]= %.2f\n", uv.Integral, numPhase+uv.Integral)
					} else {
						numPhase -= uv.Integral
						userBettingStr += fmt.Sprintf("[%.2f]= %.2f\n", uv.Integral, numPhase)
					}
				case global.BG_ZHENG:
					userBettingStr += strconv.Itoa(uv.Num[0]) + "正"
					if answerNum == uv.Num[0] {
						bgOod := global.BG_ODDS_ZHENG - 1
						numPhase += uv.Integral * float64(bgOod)
						numPhase -= float64(bgOod) * uv.Integral * v.GroupInfo.Pump
						userBettingStr += fmt.Sprintf("[%.2f]= %.2f\n", uv.Integral, numPhase+uv.Integral)
					} else if (answerNum+2)%4 != uv.Num[0]%4 {
						numPhase += 0
						userBettingStr += fmt.Sprintf("[%.2f]= %.2f\n", uv.Integral, numPhase+uv.Integral)
					} else {
						numPhase -= uv.Integral
						userBettingStr += fmt.Sprintf("[%.2f]= %.2f\n", uv.Integral, numPhase)
					}
				case global.BG_FAN:
					userBettingStr += strconv.Itoa(uv.Num[0]) + "番"
					if answerNum == uv.Num[0] {
						bgOod := global.BG_ODDS_FAN - 1
						numPhase += uv.Integral * float64(bgOod)
						numPhase -= float64(bgOod) * uv.Integral * v.GroupInfo.Pump
						userBettingStr += fmt.Sprintf("[%.2f]= %.2f\n", uv.Integral, numPhase+uv.Integral)
					} else {
						numPhase -= uv.Integral
						userBettingStr += fmt.Sprintf("[%.2f]= %.2f\n", uv.Integral, numPhase)
					}
				case global.BG_NIAN:
					userBettingStr += strconv.Itoa(uv.Num[0]) + "念" + strconv.Itoa(uv.Num[1])
					if answerNum == uv.Num[0] {
						bgOod := global.BG_ODDS_NIAN - 1
						numPhase += uv.Integral * float64(bgOod)
						numPhase -= float64(bgOod) * uv.Integral * v.GroupInfo.Pump
						userBettingStr += fmt.Sprintf("[%.2f]= %.2f\n", uv.Integral, numPhase+uv.Integral)
					} else if answerNum == uv.Num[1] {
						numPhase += 0
						userBettingStr += fmt.Sprintf("[%.2f]= %.2f\n", uv.Integral, numPhase+uv.Integral)
					} else {
						numPhase -= uv.Integral
						userBettingStr += fmt.Sprintf("[%.2f]= %.2f\n", uv.Integral, numPhase)
					}
				}
				userPhase += numPhase
			}
			userBettingStr = fmt.Sprintf("◆【%s】:%.2f\n", gv.Name, userPhase) + userBettingStr
			sumIntegral += userPhase
			userBettingStr += "\n\n"
			usersBettingStr += userBettingStr
		}
		sendMsg += fmt.Sprintf("本期盈亏:[%.2f]\n", sumIntegral)
		sendMsg += fmt.Sprintf("超级号码:%d【%d】摊\n", superNum, answerNum)
		sendMsg += "---------------\n"
		sendMsg += usersBettingStr
		sendMsg += "---------------\n"
		v.Group.SendText(sendMsg)
	}
}

// 发送每期图片
func VeryAnserIdImgReq(answerId int) {
	for _, v := range ROBOTS {
		if v.GroupInfo == nil || v.Bot == nil || v.Group == nil {
			continue
		}
		fanshuImgUrl := global.GVL_CFG.Local.HeadUrl + strconv.Itoa(answerId) + "_fanshu.png"
		if _, err := os.Stat(fanshuImgUrl); err != nil {
			utils.Screenshot(fanshuImgUrl, 220, 80, 780, 570)
		}
		fanshuimg, _ := os.Open(fanshuImgUrl)
		defer fanshuimg.Close()
		v.Group.SendImage(fanshuimg)

		//历史记录图片
		historyImgUrl := global.GVL_CFG.Local.HeadUrl + strconv.Itoa(answerId) + "_history.png"
		if _, err := os.Stat(historyImgUrl); err != nil {
			utils.Screenshot(historyImgUrl, 1250, 80, 500, 300)
		}
		historyimg, _ := os.Open(historyImgUrl)
		defer historyimg.Close()
		v.Group.SendImage(historyimg)
	}
}

// 发送用户积分
func SendUserScore() {
	for _, v := range ROBOTS {
		if v.GroupInfo == nil || v.Bot == nil || v.Group == nil {
			continue
		}
		var sendMsg string
		userInfo := server.ServerGroupUser.QueryUserScoreNotZero(v.GroupInfo.Id)
		sendMsg += fmt.Sprintf("答题人数:[%d]\n", len(userInfo))
		sumScore := 0.00
		var userInfoStr string
		for _, v := range userInfo {
			sumScore += v.SumScore
			userInfoStr += fmt.Sprintf("⭐%s💰:%.2f\n", v.Name, v.SumScore)
		}
		sendMsg += fmt.Sprintf("答题总分:%.2f\n", sumScore)
		sendMsg += userInfoStr
		v.Group.SendText(sendMsg)
	}
}

// 上分成功消息成功
func UpwardDivisionSuc(groupLoginToken string, username string, score float64) {
	groupInfo, ok := ROBOTS[groupLoginToken]
	if ok {
		sendMsg := fmt.Sprintf("@%s 上分[%.0f]成功", username, score)
		groupInfo.Group.SendText(sendMsg)
	}
}

// 上分成功消息失败
func UpwardDivisionFail(groupLoginToken string, username string, score float64) {
	groupInfo, ok := ROBOTS[groupLoginToken]
	if ok {
		sendMsg := fmt.Sprintf("@%s 上分[%.0f]失败", username, score)
		groupInfo.Group.SendText(sendMsg)
	}
}

// 下分成功消息成功
func LowerDivisionSuc(groupLoginToken string, username string, score float64) {
	groupInfo, ok := ROBOTS[groupLoginToken]
	if ok {
		sendMsg := fmt.Sprintf("@%s 下分[%.0f]成功", username, score)
		groupInfo.Group.SendText(sendMsg)
	}
}

// 下分成功消息失败
func LowerDivisionFail(groupLoginToken string, username string, score float64) {
	groupInfo, ok := ROBOTS[groupLoginToken]
	if ok {
		sendMsg := fmt.Sprintf("@%s 下分[%.0f]失败", username, score)
		groupInfo.Group.SendText(sendMsg)
	}
}

func EndDayGame() {
	for _, v := range ROBOTS {
		if v.GroupInfo == nil || v.Bot == nil || v.Group == nil {
			continue
		}
		v.Group.SendText("------今日游戏结束，明天继续----------")
	}
}
